﻿namespace CN2P.Tests
{
    using System.Linq;

    using Microsoft.VisualStudio.TestTools.UnitTesting;

    [TestClass]
    public class CartServiceTest : BaseServiceTest
    {
        [TestMethod]
        public void cartGetCartOfAbsentUserTest()
        {
            var userId = GetRandomUserId();
            var model = this.Service.CartService.GetCartOfAbsentUser(userId);
            Assert.AreEqual(0, model.Items.Count);
        }

        [TestMethod]
        public void cartAddCartTest()
        {
            var userId = GetRandomUserId();

            var cart = this.CreateRandomCart(userId);

            var model = this.Service.CartService.GetCart(userId);
            Assert.AreEqual(userId, model.UserId);
            Assert.AreEqual(cart.Items.Count, model.Items.Count);

            foreach (var originalItem in cart.Items)
            {
                Assert.IsTrue(model.Items.Any(item => item.ProdId == originalItem.ProdId));
            }
        }

        [TestMethod]
        public void cartAddAndDeleteTest()
        {
            var userId = GetRandomUserId();
            this.CreateRandomCart(userId);

            var prod1 = this.CreateRandomProduct();
            var prod2 = this.CreateRandomProduct();

            var item1 = prod1.CreateCartItem();
            var item2 = prod2.CreateCartItem();

            this.Service.CartService.PutItem(userId, item1.ProdId, item1);
            this.Service.CartService.PutItem(userId, item2.ProdId, item2);

            this.Service.CartService.DeleteCart(userId);
            var model = Service.CartService.GetCartOfAbsentUser(userId);
            Assert.AreEqual(0, model.Items.Count);
        }

        [TestMethod]
        public void cartPutNewItemTest()
        {
            var userId = GetRandomUserId();
            var cart = this.CreateRandomCart(userId);

            var prod = this.CreateRandomProduct();
            var cartItem = prod.CreateCartItem();
            this.Service.CartService.PutItem(userId, prod.ProdId, cartItem);

            var model = Service.CartService.GetCart(userId);
            Assert.AreEqual(cart.Items.Count + 1, model.Items.Count);
        }

        [TestMethod]
        public void cartPutExisitingItemTest()
        {
            var userId = GetRandomUserId();

            var prod = this.CreateRandomProduct();
            var cartItem = prod.CreateCartItem();

            this.Service.CartService.PutItem(userId, prod.ProdId, cartItem);
            cartItem.Quantity = 10;
            this.Service.CartService.PutItem(userId, prod.ProdId, cartItem);

            var model = this.Service.CartService.GetCart(userId);
            Assert.AreEqual(1, model.Items.Count);
            var item = model.Items[0];
            Assert.AreEqual(10, item.Quantity);
        }

        [TestMethod]
        public void cartAddItemTest()
        {
            var userId = GetRandomUserId();
            var prod = this.CreateRandomProduct();

            var cartItem = prod.CreateCartItem();
            this.Service.CartService.PostItem(userId, cartItem);

            var model = this.Service.CartService.GetCart(userId);
            Assert.AreEqual(1, model.Items.Count);
            var item = model.Items[0];
            Assert.AreEqual(1, item.Quantity);
            Assert.AreEqual(prod.ProdId, item.ProdId);
        }

        [TestMethod]
        public void cartPostExisitingItemTest()
        {
            var userId = GetRandomUserId();

            var prod = this.CreateRandomProduct();
            var cartItem = prod.CreateCartItem();

            this.Service.CartService.PostItem(userId, cartItem);
            cartItem.Quantity = 10;
            this.Service.CartService.PostItem(userId, cartItem);

            var model = this.Service.CartService.GetCart(userId);
            Assert.AreEqual(1, model.Items.Count);
            var item = model.Items[0];
            Assert.AreEqual(10, item.Quantity);
        }
    }
}
